<?php
namespace Home\Model;
//物品基础属性
class Container extends UserBase{
    protected $redis;
    protected $user;
    protected $username ;
    
    //加
    public function add($name, $amount){
        //统计
        $this->hIncrBy($name.'_count', $amount);
        //当前类总计数量
        $this->hIncrBy('current', $amount);
        return $this->hIncrBy($name, $amount);
    }
    //减
    public function dec($name, $amount, $pre = false){
        if($pre){
            //检查
            $r = $this->hGet($name);
            if($r >= $amount){
                return true;
            }else{
                return false;
            }
        }
        //当前类总数量
        $this->hIncrBy('current', -$amount);
        $r = $this->hIncrBy($name, -$amount);
        if($r < 0){
            $this->hIncrBy('current', $amount);
            $this->hIncrBy($name, $amount);
            return error('not enough: '.$name);
        }else{
            return $r;
        }
    }

    //上升上限
    public function up($amount){
        return $this->hIncrBy('max', $amount);
    }
    //下降上限
    public function down($amount){
        return $this->hIncrBy('max', -$amount);
    }
    public function getMax($name = 'max'){
        return $this->hMGet(array('max', 'current'));
    }
} 